/**
 * This file contains the System Encounter View class.
 */
package edu.gatech.spacetraderteam3.view;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import edu.gatech.spacetraderteam3.model.Actor;
import edu.gatech.spacetraderteam3.model.Encounter;
import edu.gatech.spacetraderteam3.model.EncounterAction;
import edu.gatech.spacetraderteam3.model.GameState;


/**
 * The Encounter screen
 * 
 * @author Alexander Ikonomidis
 * @version 1.0
 */
public class ViewSystemEncounter extends ViewSystem {
    /**
     * The windows for the buttons and for the message.
     */
    private final SGGWindow buttonWindow, messageWindow;

    /**
     * The buttons to show the user.
     */
    private final SGGButton[] buttons; // b1, b2, b3, b4;

    /**
     * The message to show the user.
     */
    private final SGGLabel messageLabel;

    /**
     * The total number of buttons to ever show.
     */
    private int numButtons = 4;

    private BufferedImage playerImage, actorImage;
    private Actor actor;

    /**
     * Initializes a system encounter screen
     */
    public ViewSystemEncounter() {
        setStarVelocity(-3.0, 0.0);
        buttonWindow = new SGGWindow(0, 200, 420, 120, GameState.ENCOUNTER);
        buttonWindow.setAlignment(SGGWindow.Alignment.CENTER);
        buttons = new SGGButton[4];
        buttons[0] = new SGGButton("Button 1!");
        buttonWindow.add(buttons[0], 30, 38);
        buttons[1] = new SGGButton("Button 2!");
        buttonWindow.add(buttons[1], 120, 38);
        buttons[2] = new SGGButton("Button 3!");
        buttonWindow.add(buttons[2], 210, 38);
        buttons[3] = new SGGButton("Button 4!");
        buttonWindow.add(buttons[3], 300, 38);
        messageWindow = new SGGWindow(0, -200, 300, 120, GameState.ENCOUNTER);
        messageWindow.setAlignment(SGGWindow.Alignment.CENTER);
        clearButtons();

        messageLabel = new SGGLabel(
                "If you see this, something was not connected properly.");
        messageLabel.setLineWidth(34);
        messageWindow.add(messageLabel, 40, 40);

        playerImage = Assets.MiscImage.SHIP_ICON.image();
    }

    /**
     * paints the encounter screen
     * 
     * @param g
     *            graphics object
     */
    public void paint(Graphics2D g) {
        super.paint(g);

        // draw player
        double imageScale = playerImage.getWidth() * 1.0 / playerImage.getHeight();
        int imageSize = 150;
        g.translate((centerX - imageSize * imageScale * 2), (centerY - imageSize
                * imageScale / 2));
        g.rotate(-Math.PI / 2);// Math.PI / 2);
        g.drawImage(playerImage, (int) (-imageSize * imageScale), 0,
                    (int) (imageSize * imageScale), (int) (imageSize * imageScale * 1.2),
                    null);
        g.rotate(Math.PI / 2);// Math.PI / 2);
        g.translate(-(centerX - imageSize * imageScale * 2), -(centerY - imageSize
                * imageScale / 2));

        // draw enemy
        if (actor != null) {
            switch (actor.getFaction()) {
                case PIRATE:
                    actorImage = Assets.MiscImage.PIRATE_ICON.image();
                    break;
                case POLICE:
                    actorImage = Assets.MiscImage.POLICE_ICON.image();
                    break;
                default:
                    actorImage = Assets.MiscImage.SHIP_ICON.image();
                    break;
            }
            // System.out.println(actorImage);
            if (actorImage != null) {
                imageScale = actorImage.getWidth() * 1.0 / actorImage.getHeight();
                g.translate((centerX + imageSize * imageScale * 2), (centerY + imageSize
                        * imageScale / 2));
                g.rotate(Math.PI / 2);// Math.PI / 2);
                g.drawImage(actorImage, (int) (-imageSize * imageScale), 0,
                            (int) (imageSize * imageScale),
                            (int) (imageSize * imageScale), null);
                g.rotate(-Math.PI / 2);// Math.PI / 2);
                g.translate(-(centerX + imageSize * imageScale * 2),
                            -(centerY + imageSize * imageScale / 2));
            }
        }

        buttonWindow.paint(g);
        messageWindow.paint(g);
    }

    /**
     * Sets the message to be displayed in the message label
     * 
     * @param message
     */
    public void setMessage(String message) {
        messageLabel.setText(message);
    }

    /**
     * sets the encounter to the appropriate encounter from the model
     * 
     * @param encounter
     */
    public void setEncounter(Encounter encounter) {
        this.actor = encounter.getActor();
        messageWindow.setVisible(true);
        buttonWindow.setVisible(true);
        messageLabel.setText(encounter.getDisplayString());
        clearButtons();
        for (EncounterAction action : encounter.getActionList()) {
            addButton(action);
        }
    }

    /**
     * adds a button to this encounter screen
     * 
     * @param action
     */
    public void addButton(final EncounterAction action) {
        if (numButtons < buttons.length - 1) {
            buttons[numButtons].setText(action.displayName());
            buttons[numButtons].setSGGEventListener(new SGGEventListener() {
                public void doEvent(SGGEvent sgge) {
                    action.doAction();
                }
            });
            buttons[numButtons].setVisible(true);
            numButtons++;
        }
    }

    /**
     * Hides the button and message windows
     */
    public void hideChoices() {
        buttonWindow.setVisible(false);
        messageWindow.setVisible(false);
    }

    /**
     * removes the individual buttons on the button window
     */
    public void clearButtons() {
        for (SGGButton b : buttons) {
            b.setVisible(false);
        }
        numButtons = 0;
    }

    /**
     * @return the encounter's display message
     */
    public String toString() {
        return messageLabel.toString();
    }
}
